// pages/shop/car/jisuanqi.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs:['全款购车','贷款购车'],
    tabIndex: 0,
    year_tags:['3年','4年','5年'],
    year_index:0,
    num_tags: ['20%','30%','40%','50%'],
    num_index: 0,
    title: null,
    price: 0,
    must_list:[
      {title:'购置税'},
      { title: '上牌费用',price:500 },
      { title: '车船使用税' ,sub:[
        {title:'1.0L（含）以下', price:300},
        { title: '1.0-1.6L（含）', price: 420 },
        { title: '1.6-2.0L（含）', price: 480, selected: true },
        { title: '2.0-2.5L（含）', price: 900 },
        { title: '2.5-3.0L（含）', price: 1920 },
        { title: '3.0-4.0L（含）', price: 3480 }
      ]},
      {
        title: '交强险', sub: [
          { title: '家用6座以下', price: 950, selected: true },
          { title: '家用6座及以上', price: 1100 },
        ]
      },
    ],
    shang_list: [
      {
        title: '第三者责任险', checked:true, sub: [
          { title: '5万', price: 516 },
          { title: '10万', price: 746, selected: true },
          { title: '20万', price: 924 },
          { title: '50万', price: 1252 },
          { title: '100万', price: 1630 }
        ]
      },
      { title: '车辆损失险', checked: true },
      { title: '全车盗抢险', checked: true },
      { title: '玻璃单独破碎险', checked: true, sub:[
        { title: '进口'},
        { title: '国产', selected:true },
      ] },
      { title: '自燃损失险', checked: true },
      { title: '不计免赔特约险', checked: true },
      { title: '无过责任险', checked: true },
      { title: '车上人员责任险', checked: true, price:50 },
      {
        title: '车身划痕险', checked: true, sub: [
          { title: '2千', price: 400 },
          { title: '5千', price: 570, selected: true },
          { title: '1万', price: 760 },
          { title: '2万', price: 1140 }
        ]
      }
    ]
  },
  setTab: function(e){
    this.setData({
      tabIndex: e.currentTarget.dataset.index
    })
    this.updateMoney();
  },
  setTag: function(e){
    if (e.currentTarget.dataset.type === 'year'){
      this.setData({
        year_index: e.currentTarget.dataset.idx
      })
    }else{
      this.setData({
        num_index: e.currentTarget.dataset.idx
      })
    }
    this.updateMoney();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      title: options.title,
      price:options.price*10000
    })
    this.updateMoney();
  },
  updateMoney: function(){
    var info = {};
    const price = this.data.price;
    var must_list = this.data.must_list;
    var shang_list = this.data.shang_list;
    must_list[0].price = parseInt((price/1.17)*0.1);
    var must_money = 0;
    must_list.forEach(function(item, idx){
      if(item.sub){
        item.sub.forEach(function(v, k){
          if(v.selected){
            must_money += v.price;
          }
        })
      }else{
        must_money += item.price;
      }
    })
    info.must_money = must_money;

    var third_price = 0;
    shang_list[0].sub.forEach(function (item, idx) {
      if(item.selected){
        third_price = item.price;
      }
    })
    shang_list[1].price = parseInt((price * 1.0880) /100) + 459;
    shang_list[2].price = parseInt((price * 0.09) / 100) + 459;
    shang_list[3].sub[0].price = parseInt((price * 0.25) / 100);
    shang_list[3].sub[1].price = parseInt((price * 0.15) / 100);
    shang_list[4].price = parseInt((price * 0.15) / 100);
    shang_list[5].price = parseInt((third_price + shang_list[1].price) * 0.2);
    shang_list[6].price = parseInt(third_price * 0.2);
    var all_checked = true;
    var shang_money = 0;
    shang_list.forEach(function (item, idx) {
      if (item.checked) {
        if (item.sub) {
          item.sub.forEach(function (v, k) {
            if (v.selected) {
              shang_money += v.price;
            }
          })
        } else {
          shang_money += item.price;
        }
      }else{
        all_checked = false
      }
    })
    info.all_checked = all_checked;
    info.shang_money = shang_money;
    info.all_money = price + must_money + shang_money;

    if(this.data.tabIndex === 1){
      var other_money = 0;
      switch(this.data.num_index){
        case 0:
          info.first_money = parseInt(price * 0.2) + must_money + shang_money;
          other_money = parseInt(price * 0.8);
          break;
        case 1:
          info.first_money = parseInt(price * 0.3) + must_money + shang_money;
          other_money = parseInt(price * 0.7);
          break;
        case 2:
          info.first_money = parseInt(price * 0.4) + must_money + shang_money;
          other_money = parseInt(price * 0.6);
          break;
        case 3:
          info.first_money = parseInt(price * 0.5) + must_money + shang_money;
          other_money = parseInt(price * 0.5);
          break;
      }
      switch (this.data.year_index) {
        case 0:
          var lilv = 5.25 / 100 / 12;
          info.all_lixi = parseInt((other_money * lilv * Math.pow((1 + lilv), 36)) / (Math.pow((1 + lilv), 36) - 1)) * 36 - other_money;
          info.mouth_price = parseInt((info.all_lixi + other_money) / 36) ;
          break;
        case 1:
          var lilv = 5.25 / 100 / 12;
          info.all_lixi = parseInt((other_money * lilv * Math.pow((1 + lilv), 48)) / (Math.pow((1 + lilv), 48) - 1)) * 48 - other_money;
          info.mouth_price = parseInt((info.all_lixi + other_money) / 48);
          break;
        case 2:
          var lilv = 5.25 / 100 / 12;
          info.all_lixi = parseInt((other_money * lilv * Math.pow((1 + lilv), 60)) / (Math.pow((1 + lilv), 60) - 1)) * 60 - other_money;
          info.mouth_price = parseInt((info.all_lixi + other_money) / 60);
          break;
      }
      info.all_money += info.all_lixi;
    }

    this.setData({
      info: info,
      must_list: must_list,
      shang_list: shang_list
    })
  },
  showSelect: function(e){
    const idx = e.currentTarget.dataset.idx;
    var itemList = [];
    var shang_list = this.data.shang_list;
    shang_list[idx].sub.forEach(function(item, key){
      itemList.push(item.title);
      shang_list[idx].sub[key].selected = false;
    })
    let that = this;
    wx.showActionSheet({
      itemList: itemList,
      success: function (res) {
        if (res.tapIndex !== undefined){
          shang_list[idx].sub[res.tapIndex].selected = true;
          that.setData({
            shang_list: shang_list
          })
          that.updateMoney();
        }
      }
    })
  },
  showMustSelect: function (e) {
    const idx = e.currentTarget.dataset.idx;
    var itemList = [];
    var must_list = this.data.must_list;
    must_list[idx].sub.forEach(function (item, key) {
      itemList.push(item.title);
      must_list[idx].sub[key].selected = false;
    })
    let that = this;
    wx.showActionSheet({
      itemList: itemList,
      success: function (res) {
        if (res.tapIndex !== undefined) {
          must_list[idx].sub[res.tapIndex].selected = true;
          that.setData({
            must_list: must_list
          })
          that.updateMoney();
        }
      }
    })
  },
  changeChecked: function(e){
    const idx = e.currentTarget.dataset.idx;
    var shang_list = this.data.shang_list;
    shang_list[idx].checked = !shang_list[idx].checked;
    this.setData({
      shang_list: shang_list
    })
    this.updateMoney();
  },
  changeAllChecked: function(){
    const all_checked = !this.data.info.all_checked;
    var shang_list = this.data.shang_list;
    shang_list.forEach(function(item, idx){
      shang_list[idx].checked = all_checked;
    })
    this.setData({
      shang_list: shang_list
    })
    this.updateMoney();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})